//
//  HWInviteRewardController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/15.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWInviteRewardController: HWBaseViewController {
    var tableView:UITableView!
    var navRightButton:UIButton!
    var tableViewProtocol = HWInviteRewardProtocol()
    var httpManager = HWInviteHttpManger()
    var mods:[HWInviteRecordModel] = []
    var model = HWInviteRechargeModel()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
    }

}

extension HWInviteRewardController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = "发放奖励"
        navigationView.backButton.setImage(UIImage(named: "navBackButton"), for: .normal)
        
        navRightButton = UIButton(type: .custom)
        navRightButton.setTitle("发放记录", for: .normal)
        navRightButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
        navRightButton.titleLabel?.font = .systemFont(ofSize: 13*widthScale)
        navRightButton.addTarget(self, action: #selector(navRightButtonClick(_:)), for: .touchUpInside)
        navigationView.addSubview(navRightButton)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
    }
    
    private func initFrame() -> Void {
        
        navRightButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.centerY.equalTo(navigationView.titleLabel)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
    }
    
}

extension HWInviteRewardController {
    func loadData() -> Void {
        self.tableView.mj_footer?.endRefreshing()
        let netGroup = DispatchGroup()
        let workingQueue = DispatchQueue(label: "InviteReward")
        
        netGroup.enter()
        workingQueue.async {
            self.httpManager.getInviteRecharge { model in
                self.model = model
                netGroup.leave()
            } failure: { errorString in
                netGroup.leave()
            }

        }
        
        netGroup.enter()
        workingQueue.async {
            self.httpManager.getInviteRecord(type: "0") { models in
                self.mods = models
                netGroup.leave()
            } failure: { errorString in
                netGroup.leave()
            }

        }
        
        netGroup.notify(queue: workingQueue) {
            DispatchQueue.main.asyncAfter(deadline: .now()) {
                self.tableViewProtocol.models = self.mods
                self.tableViewProtocol.model = self.model
                self.tableView.reloadData()
                self.tableView.mj_header?.endRefreshing()
            }
            
        }
    }
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_header?.endRefreshing()
        self.tableView.mj_footer?.endRefreshing()
        self.httpManager.getInviteRecordFooter(type:"0") { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                for mod in models {
                    self.mods.append(mod)
                }
            }
            self.tableViewProtocol.models = self.mods
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
}

extension HWInviteRewardController {
    func addAction() -> Void {
        tableViewProtocol.sendButtonClick = {
            mod in
            let rewardSheet = HWInviteRewardSheet()
            rewardSheet.show()
            
            rewardSheet.sureButtonClick = {
                txt in
                self.httpManager.getInviteRewardSend(down_uid: mod.user_id, money: txt) { isSuccess in
                    self.view.showHUD(title: "发放成功")
                    rewardSheet.hide()
                    self.loadData()
                } failure: { errorString in
                    self.showHUD(title: errorString)
                }

            }
        }
    }
    
    @objc func navRightButtonClick(_:UIButton) -> Void {
        self.navigationController?.pushViewController(HWInviteRecordController(), animated: true)
    }
}
